package com.itfitness.idcardscan.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;

/**
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2022/4/12 11:15
 * @UpdateUser: 更新者：
 * @UpdateDate: 2022/4/12 11:15
 * @UpdateRemark: 更新说明：
 */
public class MaskView extends View {
    private Paint mPaint,mPaintScanLine;
    private Paint mPaint_Text;
    private RectF mCardRectF;
    private float mCardWidth;
    private float mCardHeight;
    private float scanLineHeight = 6;//扫描线高度
    private ValueAnimator scanLineAnimator;
    private float scanLineTop;

    public MaskView(Context context) {
        this(context,null);
    }

    public MaskView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MaskView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setLayerType(LAYER_TYPE_SOFTWARE,null);//关闭硬件加速
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);

        mPaintScanLine = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaintScanLine.setStyle(Paint.Style.FILL);
        mPaintScanLine.setColor(Color.WHITE);

        mPaint_Text = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_Text.setTextSize(50);
        mPaint_Text.setColor(Color.WHITE);
        mPaint_Text.setStrokeWidth(3);
        mPaint_Text.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //由于身份证的长为85.6mm，身份证的宽为54mm因此我们可以得出身份证的长宽比
        float ratio = (float) (85.6 / 54);
        //这里我们设置显示身份证区域的宽为整个控件宽度的2/3
        mCardWidth = w / 7 * 3.7f;
        //因此可以得出身份证区域的高度为
        mCardHeight = mCardWidth / ratio;
        //接下来可以算出身份证区域的矩形位置为
        mCardRectF = new RectF((w - mCardWidth) / 2, (h - mCardHeight) / 2, w - (w - mCardWidth) / 2, h - (h - mCardHeight) / 2);
        scanLineTop = mCardRectF.top;
        startScanLineAnim();
    }

    /**
     * 启动扫描线动画
     */
    private void startScanLineAnim() {
        if(scanLineAnimator != null){
            scanLineAnimator.cancel();
        }
        scanLineAnimator = ValueAnimator.ofFloat(mCardRectF.top, mCardRectF.bottom - scanLineHeight);
        scanLineAnimator.setDuration(3000);
        scanLineAnimator.setInterpolator(new LinearInterpolator());
        scanLineAnimator.setRepeatCount(ValueAnimator.INFINITE);
        scanLineAnimator.setRepeatMode(ValueAnimator.REVERSE);
        scanLineAnimator.addUpdateListener(animation -> {
            scanLineTop = (float) animation.getAnimatedValue();
            postInvalidate();
        });
        scanLineAnimator.start();
    }

    /**
     * 获取扫描区域
     * @return
     */
    public RectF getScanRect(){
        if(mCardRectF == null){
            return new RectF(getLeft(),getTop(),getRight(),getBottom());
        }
        return mCardRectF;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();//保存画布
        canvas.drawColor(Color.parseColor("#aa000000"));//填充黑色半透明
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//设置混合模式为清除
        canvas.drawRect(mCardRectF,mPaint);
        mPaint.setXfermode(null);//清除混合模式
        canvas.restore();//恢复画布
        drawScanLine(canvas);
        canvas.translate(getWidth()/2,getHeight()/2);
        canvas.drawText("请将身份证对准扫描框",0,-mCardHeight/2 - mPaint_Text.getTextSize() / 2,mPaint_Text);
    }

    /**
     * 画扫描线
     * @param canvas
     */
    private void drawScanLine(Canvas canvas) {
        canvas.drawOval(mCardRectF.left,scanLineTop,mCardRectF.right,scanLineTop + scanLineHeight,mPaintScanLine);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(scanLineAnimator != null){
            scanLineAnimator.cancel();
        }
    }
}
